const db = wx.cloud.database();
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    index2: 0,
    index3: 0,
    index4: 0,
    dishi: ['请选择高校所在地市'],
    gaoxiao: ['请选择高校'],
    xiaoqu: ['请选择所属校区'],
    nianji: ['请选择所在年级', '大一', '大二', '大三', '大四', '研究生', ],
    phone: 0,
    theList: ''
  },


  bindPickerChange: function (e) {
    let num = e.detail.value;
    let that = this;
    console.log('dishi发送选择改变，携带值为', that.data.dishi[num]);
    this.setData({
      index: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=41828&callback=?', //接收url
      data: {
        level: '2',
        grfiled: 'dishi',
        grtext: that.data.dishi[num]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        var list = [];
        list.push('请选择高校')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].gaoxiao)
        }
        that.setData({
          gaoxiao: list
        });
      }
    })
  },
  bindPickerChange2: function (e) {
    let num2 = e.detail.value;
    let that = this;
    console.log('gaoxiao发送选择改变，携带值为', that.data.gaoxiao[num2]);
    this.setData({
      index2: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=41828&callback=?', //接收url
      data: {
        level: '3',
        grfiled: 'gaoxiao',
        grtext: that.data.gaoxiao[num2],
        onefiled: 'dishi',
        onetext: that.data.dishi[this.data.index]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson)
        var list = [];
        list.push('请选择所属校区')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].xiaoqu)

        }
        that.setData({
          xiaoqu: list
        });
      }
    })
  },
  bindPickerChange3: function (e) {
    let num3 = e.detail.value;
    let that = this;
    console.log('xiaoqu发送选择改变，携带值为', that.data.xiaoqu[num3]);
    this.setData({
      index3: e.detail.value
    })

  },

  nianji: function (e) {
    let num4 = e.detail.value;
    let that = this;
    console.log('nianji发送选择改变，携带值为', that.data.nianji[num4]);
    this.setData({
      index4: e.detail.value
    })

  },


  onLoad: function (options) {
    var that = this;
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=41828&callback=?', //接收url
      data: {
        level: '1',
        dishi: ''
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson)
        var list = [];
        list.push('请选择高校所在地市')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].dishi)
        }
        that.setData({
          dishi: list
        });
      }
    })
    var that = this;
    console.log(options);
    that.setData({
      area: options.area,
      geneal: options.agent
    })
    if (options.page) {
      that.setData({
        page: options.page
      })
    }
  },


  getPhoneNumber(e) {
    var that = this;
    var dishi = that.data.dishi[that.data.index];
    var gaoxiao = that.data.gaoxiao[that.data.index2];
    var xiaoqu = that.data.xiaoqu[that.data.index3];
    var nianji = that.data.nianji[that.data.index4];
    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {

      wx.request({
        url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
        method: 'POST',
        data: {
          activeId: 33,
          mobile: res.result,
          areaCode: that.data.area,
          agent: that.data.geneal,
          pageId: that.data.page
        },
        success: res => {
          console.log(res);
        }
      })
      console.log(that.data.area)
      console.log(that.data.geneal)
      wx.request({ //发送请求
        url: 'https://zg99.offcn.com/index/chaxun/register?actid=41828&callback=?', //接收url
        data: {
          dishi: dishi,
          gaoxiao: gaoxiao,
          xiaoqu: xiaoqu,
          nianji:nianji,
          phone: res.result,
          fenxiao: that.data.area,
          geneal: that.data.geneal,
        }, //接收参数
        success(res) {
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          }
          console.log(theJson.lists[0])
          that.setData({
            list: theJson.lists[0]
          })
          wx.pageScrollTo({
            scrollTop: 1400,
            duration: 300
          });


        }
      })
    }).catch(err => {
      console.error(err);
    });


  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})